package HashTable._690_EmployeeImportance;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class _690_EmployeeImportance {
    //hint 1:if the employee's id is occur in order,then can create a array and add every subordinate's id into it every iterate,
    //and use a position point the point the current locaiton
    //quesiton:what about more than 2 employee have the same subordinates?for example:1 and 2 both have a subordinate which's id is 3

    //use hashmap to faster the process of search the employee and his subordinates' id.
    Map<Integer, Employee> maps = new HashMap<>();
    public int getImportance(List<Employee> employees, int id) {
        for (Employee e : employees) {
            maps.put(e.id,e);
        }
        return dfs(id);
    }

    public int dfs(int id) {
        int sum = (maps.get(id)).importance;
        for (Integer subid : maps.get(id).subordinates) {
            sum += dfs(subid);
        }
        return sum;
    }
}
